var index;
var isEdit = false;
$(function(){
  initTab("");
  resourceBtnInit();
  searchBtnInit();
  delteBtn();
  $("#resourceTableDiv").on("click", ".editBtn", function () {
    var resourceId = $(this).attr("resourceId");
    $("#resourceId").val(resourceId);
    isEdit = true;
    $.ajax({
      type: "get",
      url: "getResourceById.do",
      data: {
        "resourceId": resourceId
      },
      success: function (data) {
        $("#name").val(data.name);
        $("#phone").val(data.phone);
        $("#professionType").val(data.professionType);
        $("#mark").val(data.mark);
        var form = layui.form;
        form.val("resourceForm", {
          "customType": data.customType
        })
        disableInput("disabled");
      }
    });
    layui.use(['layer'],function () {
      var layer = layui.layer,$=layui.$;
      index = layer.open({
        type:1,//类型
        area: ['auto', 'auto'],//定义宽和高
        title:'添加资源',//题目
        shadeClose:false,//点击遮罩层关闭
        zIndex: 1,
        content: $('#layerOpen')//打开的内容
      });
    });
  });
});


function disableInput(val) {
  if(val == "disabled"){
    $("#name").attr("disabled",val);
    $("#phone").attr("disabled",val);
    $("#professionType").attr("disabled",val);
  } else {
    $("#name").removeAttr("disabled");
    $("#phone").removeAttr("disabled");
    $("#professionType").removeAttr("disabled");
    $("#name").val("");
    $("#phone").val("");
    $("#professionType").val("");
    $("#mark").val("");
    var form = layui.form;
    form.val("resourceForm", {
      "customType": ""
    })
  }

}

function delteBtn() {
  $("#resourceTableDiv").on("click", ".deleteBtn", function () {
    var resourceId = $(this).attr("resourceId");
    swal({
      title: "你确定要删除该资源？",
      type: "warning",
      allowOutsideClick: true,
      showConfirmButton: true,
      showCancelButton: true,
      confirmButtonClass: "layui-btn layui-btn-warm",
      cancelButtonClass: "layui-btn layui-btn-primary",
      confirmButtonText: "确认",
      cancelButtonText: "取消"
    }, function (o) {
      if (o) {
        $.ajax({
          type: "get",
          url: "deleteResourceById.do",
          data: {
            "resourceId": resourceId
          },
          success: function (data) {
            if (data == "success") {
              tablerelaod("resourceTab");
              toastrComponent.show("success", "删除成功");
            }
          }
        });
      }
    });
  });
}


function searchBtnInit() {
	$("#searchBtn").on("click",function () {
		var serachStr = $("#searchStr").val();
    initTab(serachStr);
  });
}

function resourceBtnInit() {

	$("#addResourceBtn").on("click",function () {
    disableInput("");
    isEdit = false;
    layui.use(['layer'],function () {
      var layer = layui.layer,$=layui.$;
      index = layer.open({
        type:1,//类型
				area: ['auto', 'auto'],//定义宽和高
        title:'添加资源',//题目
        shadeClose:false,//点击遮罩层关闭
        zIndex: 1,
        content: $('#layerOpen')//打开的内容
      });
    });

    $("#name").focus();
  });

  $("#cancelBtn").on("click", function () {
    layer.close(index);
  });
  
  $("#saveResourceBtn").on("click",function () {
		var name = $("#name").val();
		var phone = $("#phone").val();
		var professionType = $("#professionType").val();
		var customType = $("#customType").val();
		var mark = $("#mark").val();

    if("" == name){
      toastrComponent.show("warning", "请填写资源名称！");
      return;
    }
    if(name.length > 50){
      toastrComponent.show("warning", "资源名称最多50个字符！");
      return;
    }
    if("" == phone){
      toastrComponent.show("warning", "请填写资源电话！");
      return;
    }
    var reg = /^[0-9]*$/;
    if(!reg.test(phone) || phone.length > 12){
      toastrComponent.show("warning", "请填写合法的电话号码！");
      return;
    }
    if("" == professionType){
      toastrComponent.show("warning", "请填写资源类型！");
      return;
    }
    if("" == customType){
      toastrComponent.show("warning", "请选择客户类型！");
      return;
    }
    if(professionType.length > 50){
      toastrComponent.show("warning", "资源类型最多50个字符！");
      return;
    }
    if(mark.length > 255){
      toastrComponent.show("warning", "备注最多255个字符！");
      return;
    }
    var url = "saveResource.do";
    var id;
    if(isEdit){
      url = "updateResourceMark.do";
      id = $("#resourceId").val();
    }

    $.ajax({
      type: "get",
      url: url,
      data: {
        "id": id,
        "name": name,
        "phone": phone,
        "professionType": professionType,
        "customType": customType,
        "mark": mark
      },
      success: function (data) {
      	if("success" == data){
          toastrComponent.show("success", "保存成功！");
          layer.close(index);
          $("#searchStr").val("");
          initTab("");
          $("#name").val("");
          $("#phone").val("");
          $("#professionType").val("");
          $("#mark").val("");
          var form = layui.form;
          form.val("resourceForm", {
            "customType": ""
          })
				}
      }
    });
  });
}


function initTab(search) {
  layui.use(['table'], function() {
    var table = layui.table;
    table.render({
      elem: '#resourceTab'
      , url: 'getResource.do?search=' + search
      , height: '480',
      page: {
        layout: ['count', 'prev', 'page', 'next', 'skip']
        , groups: 1 //只显示 1 个连续页码
        , first: false //不显示首页
        , last: false //不显示尾页
        , limit: 10
        , limits: [10]
      }
      , cols: [[
        {field: 'name', align: 'center', width: '18%', title: '名称'}
        ,{field: 'phone', align: 'center', width: '12%', title: '电话'}
        , {field: 'professionType', align: 'center', width: '15%',title: '专业类型'}
        , {field: 'createTime', align: 'center', width: '15%', title: '录入时间'}
        , {field: 'createUserName', align: 'center', width: '10%', title: '录入人员'}
        , {field: 'customType', align: 'center', width: '10%', title: '客户类型',
          templet: function (data) {
          var html = "";
          if(data.customType == "重点客户"){
            html="<span style='color: red;'>重点客户</span>";
          }else if(data.customType == "意向客户"){
            html="<span style='color: blue;'>意向客户</span>";
          }else if(data.customType == "普通客户"){
            html="<span style='color: green;'>普通客户</span>";
          }
          return html;
        }}
        , {field: 'mark', align: 'center', width: '12.3%', title: '备注'}
        , {
          field: 'id', align: 'center', width: '8%', title: '操作',
          templet: function (data) {
            var loginUserRoleId = $("#loginUserRoleId").val();
            var loginUserId = $("#userId").val();
            var html = '';
            if(data.createUserId == loginUserId){
              html += '<a class="layui-btn layui-btn-xs editBtn" lay-event="edit" resourceId='+data.id+'>编辑</a>';
            }else{
              html += '<a class="layui-btn layui-btn-xs" lay-event="edit">无操作</a>';
            }
            return html;
          }
        }
      ]]
    });
  })
}


function tablerelaod(tableId) {
  var table = layui.table;
  table.reload(tableId);
}